package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Description:
 * User: 故我
 * Date: 2023-11-14
 * Time: 17:56
 */
@RestController
@RequestMapping("/user")
public class UserController {
    // 使用代码进行 redis 操作
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 向redis中存储数据
     */
    @RequestMapping("/set")  // key  value
    public String setRedis(String username, String password) {
        stringRedisTemplate.opsForValue().set(username, password);
        return "设置 redis success!";
    }

    /**
     * 读取 redis 中的值
     */
    @RequestMapping("/get")
    public String getRedis(String username) {
        Object object = stringRedisTemplate.opsForValue().get(username);
        if (object != null) {
            return object.toString();
        }
        return "Null";
    }
}
